<?php

function __autoload($class_name) {
    include $class_name . '.php';
}

class pFunction {

    public $name;
    public $type;

    function __construct($name, $type) {
        $this->name = $name;
        $this->type = $type;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

}

class pVariable {

    public $name;
    public $type;

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

}

class parseResults {
    
    public $className;
    public $comments;
    public $variables;
    public $functions;

    public function addVariable($variable) {
        $this->variables[] = $variable;
    }

    public function getClassName() {
        return $this->className;
    }

    public function setClassName($className) {
        $this->className = $className;
    }

    public function getComments() {
        return $this->comments;
    }

    public function setComments($comments) {
        $this->comments = $comments;
    }

    public function getVariables() {
        return $this->variables;
    }

    public function setVariables($variables) {
        $this->variables = $variables;
    }

    public function addFunctions($functions) {

        foreach ($functions as $func) {
            $this->functions[] = $func;
        }
    }

    public function getFunctions() {
        return $this->functions;
    }

    public function setFunctions($functions) {
        $this->functions = $functions;
    }

}
    
class classParse {

    public $inputFileName;
    public $outputFileName;
    public $data;
    public $result;

    public function __construct($fileName) {
        $this->inputFileName = $fileName;
        $this->outputFileName = $this->inputFileName . ".bak";

        $res = str_replace("\n", " ", htmlspecialchars(
                        file_get_contents($this->inputFileName)
                )
        );

        $this->data = $res;
        $this->data = preg_replace('/ +/', ' ', $this->data);
        $this->result = new parseResults();

        $this->getComments();
        $this->getFunctions();
        $this->getVariables();
    }

    protected function _getStrBetweenValues($val1, $val2) {

        $regexp = "/" . $val1 . "(.*?)" . $val2 . "/";
        preg_match_all($regexp, $this->data, $result);

        return $result[1];
    }

    public function getClassNames() {
        
        $regexp = "/" . " class " . "(.+?)" . "{" . "/";
        preg_match_all($regexp, str_replace("\n", " ",file_get_contents($this->inputFileName)), $result);
//        preg_match_all($regexp, $this->data, $result);

        $results = array();
        foreach ($result[1] as $res) {
            preg_match("/\w+/", $res, $tmp);
            $results [] = $tmp[0];
        }
        
        $results = $this->getNotNullList($results);
        
        return $results;
    }
    
    public function getVariables() {
        
        $use = array();

        $content = file_get_contents($this->inputFileName);
        $content = explode("\n", $content);
        foreach ($content as $key=>$value) {
            $use[] = $value;  
        }
        
        $keys = array();
        
        foreach ($use as $key=>$value) {
            if (preg_match("/function/", $value)) $keys[] = $key;  
        }
        $keys[] = count($use);
 
//==============================================================================        
        
        //get class variables
        $result = array();
        for($i=0;$i<$keys[0]-1;$i++) {
            if(preg_match("/[\$]/", $use[$i])) {
                $v = preg_replace("/^(.*)[\$]/", "", $use[$i]);
                $v = preg_replace("/[\;]/", "", $v);
            }    
            if($v != null) $result['variables'][] = $v;
        }
//==============================================================================
for($z=0;$z<count($keys)-1;$z++) {

        //get arguments
        for($i=$keys[$z];$i<$keys[$z]+1;$i++) {
            $v = preg_replace("/^(.*)\(/", "", $use[$i]);
            $v = preg_replace("/\)(.*)/", "", $v);
            $v = preg_replace("/[\$]/", "", $v);
            $v = explode(", ", $v);
            if($v != null) $result['arguments'][] = $v;
        }
        
        for($i=$keys[$z]+1;$i<$keys[$z+1]-1;$i++) {
            $v = preg_replace("/[ \t]+/", "", $use[$i]);
            $v = preg_replace("/[\}]/", "", $v);
            if($v != null) $result['code_temp'][] = $v;
        }
        //bind code
        $result['code'][$z] = $result['code_temp'];
        unset ($result['code_temp']);
}
//============================================================================== 

        $this->result->arguments = $result['arguments'];
        $this->result->variables = $result['variables'];
        $this->result->code = $result['code'];
        
        return $this;

    }

    public function getComments() {

        $strs = $this->_getStrBetweenValues("\/\*", "\*\/");
        foreach ($strs as $value) {
            $all[] = explode('*', $value);
        }

        $result = array();
        foreach ($all as $subArr) {
            $tmpArray = array();

            foreach ($subArr as $subValue) {

                if (strlen(preg_replace('/ +/', ' ', $subValue)) > 1) {
                    $tmpArray[] = $subValue;
                }
            }

            $result[] = $tmpArray;
        }

        $resultSet = array();
        foreach ($result as $subArray) {
            $resultSet[] = implode(" ", $subArray);
        }

        $this->result->setComments($resultSet);
    }

    public function getFunctions() {
        
        $funcTypes = array('public', 'protected', 'public');
        
        $allFuncts = array();
        $allNames = array();
       
   
        $defaultsFunctions = $this->_getStrBetweenValues("function ", "\(");
            if ($defaultsFunctions) {
                foreach ($defaultsFunctions as $func) {
                    if(!in_array($func, $allNames)){
                        $tmpFunc = new pFunction($func, "public");
                        $allFuncts[] = $tmpFunc;
                        $allNames[] = $func;
                    }
                }
            } 
            
       foreach ($funcTypes as $type) {
            $protectedFunctions = $this->_getStrBetweenValues("$type function ", "\(");

            if ($protectedFunctions) {
                foreach ($protectedFunctions as $func) {
                    if(!in_array($func, $allNames)){
                        $tmpFunc = new pFunction($func, $type ? $type : "public");
                        $allNames[] = $func;
                        $allFuncts[] = $tmpFunc;
                    }
                }
            }
        }     
            
        $this->result->addFunctions($allFuncts);
        
        
        
        $strs = $this->_getStrBetweenValues('function ', '\(');
        
        $searchString = "/function/";
    
        $file = file_get_contents($this->inputFileName);
 
        
        $data = explode("\n", $file);

        for ($line = 0; $line < count($data); $line++) {
            if (preg_match($searchString, $data[$line])) {
                
            }
        }
        
        $resultSet = array();
        foreach ($allNames as $value) {
            
            preg_match("/\w+/", $value, $str);
            
            $resultSet[]=$str[0];
        }
        
        
        return $resultSet;
//        $this->result->setFunctions($this->_getStrBetweenValues("function", "\("));    
    }

    public function process() {

        $this->result->setClassName(current($this->_getStrBetweenValues('class', '{')));


        $classBody = $this->_getStrBetweenValues('class', '\?\&gt;');

        $regexp = "/(public|protected|public)/";

        preg_match_all($regexp, $this->data, $res);
    }

    public function getResult() {
        return $this->result;
    }
    
    public function getNotNullList($array){

    $result= array();
    foreach($array as $key=>$value){
        
        $valueTmp = preg_replace('/ +/', '', $value);
        if(strlen($valueTmp)>1){
            $result[$key]= $value;
        }
    }
    return $result;
    
}


}



?>


